package com.example.qmzy_202208190217.ui.place

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import com.example.qmzy_202208190217.logic.Repository
import com.example.qmzy_202208190217.logic.model.Place

// ViewModel访问仓库层
class PlaceViewModel : ViewModel() {

    // 用于存储查询字符串的MutableLiveData
    private val searchLiveData = MutableLiveData<String>()
    val placeList = ArrayList<Place>()

    // 使用switchMap将查询结果转换为一个LiveData对象
    val placeLiveData = searchLiveData.switchMap { query ->
        // ViewModel访问仓库层，根据查询字符串获取位置列表
        Repository.searchPlaces(query)
    }

    // 搜索位置的方法
    fun searchPlaces(query: String) {
        searchLiveData.value = query
    }

    fun savePlace(place: Place) = Repository.savePlace(place)
    fun getSavedPlace() = Repository.getSavedPlace()
    fun isPlaceSaved() = Repository.isPlaceSaved()
}